package cn.edu.ctbu.firstdp.service;

import cn.edu.ctbu.firstdp.constant.REnum;
import cn.edu.ctbu.firstdp.dao.StudentRepository;
import cn.edu.ctbu.firstdp.entity.Student;
import cn.edu.ctbu.firstdp.exception.RException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
public class StudentService {
    //自动new对象
    @Autowired
    public StudentRepository studentRepository;
    /**
     * 读学生列表的所有信息
     * @return List Student
     */
    public List<Student> getALL(){

        return studentRepository.findAll();
    }

    /**
     * 实现分页
     * @param pageable
     * @return
     */
    public Page<Student> getALL(Pageable pageable){

        return studentRepository.findAll(pageable);
    }
    public Page<Student> getALL(Example<Student> example,Pageable pageable){

        return studentRepository.findAll(example,pageable);
    }

    /**
     * 按id进行查询
     * @param id 主键,整数
     * @return List Student
     */

    public Student findById(Integer id){

        return studentRepository.findById(id).get();
    }


    public List<Student> findBySno(String sno){
        return studentRepository.findBySno(sno);
    }

    /**
     * 按名字进行查询，like
     * @param name 主键,整数
     * @return List Student
     */

    public List<Student> findByName(String name){

        return studentRepository.findByNameLike(name);
    }


    /**
     * 按名字和密码查询
     * @param name
     * @param password
     * @return
     */
    public List<Student> findByNameAndPassword(String name,String password){

        return studentRepository.findByNameAndPassword(name,password);
    }

    public void insert(Student student){
        studentRepository.save(student);
    }

    public void update(Student student){
        studentRepository.save(student);
    }

    public void delete(Integer id){
        studentRepository.deleteById(id);
    }


    /**
     * 添加学生
     * @param student
     * @return
     */
    public Student add(Student student){
        return studentRepository.save(student);
    }

    /**
     * 更新学生
     * @param student
     * @return
     */
    public Student update1(Student student){
        return studentRepository.save(student);
    }

    /**
     * 删除学生
     * @param id
     * @return
     */
    public void delete1(Integer id){
         studentRepository.deleteById(id);
    }

    public void delete2(List<Integer> ids) {
        // 根据 id 列表删除学生
        ids.forEach(id -> studentRepository.deleteById(id));
    }

    public Student validateUsernameAndPassword(String sno, String password) throws Exception {

        List<Student> students = studentRepository.findBySno(sno);
        if (students.size() > 0) {
            //可能对 password加密， 但我们暂时不做处理
            Student student = students.get(0);
            if (student.getPassword().equals(password)) {
                //成功
                return student;
            } else {
                throw new RException(REnum.LOGIN_ERR);
            }
        } else {
            throw new RException(REnum.LOGIN_ERR);
        }
    }


}
